package com.yy.young.travel.web;

import com.yy.young.common.util.CommonUtil;
import com.yy.young.common.util.Result;
import com.yy.young.common.util.StringUtils;
import com.yy.young.dal.util.Page;
import com.yy.young.fs.model.FileInfo;
import com.yy.young.fs.service.IFileService;
import com.yy.young.interfaces.log.annotation.Log;
import com.yy.young.travel.model.TravelFile;
import com.yy.young.travel.service.ITravelFileService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 旅游文件服务
 * Created by rookie on 2018-03-23.
 */
@Controller
@RequestMapping("/travel/travelFile")
public class TravelFileController {

    @Resource(name="travelFileService")
    ITravelFileService service;

    @Resource(name = "fsFileService")
    IFileService fileService;

    /**
     * 获取数据列表
     * @param travelFile
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询旅游文件列表")
    @RequestMapping("/getTravelFileList")
    @ResponseBody
    public Object getTravelFileList(TravelFile travelFile, HttpServletRequest request) throws Exception{
        List<TravelFile> list = service.getTravelFileList(travelFile);
        return new Result(list);
    }

    /**
     * 根据资源编号和类型查询
     * @param travelFile
     * @param request
     * @return
     * @throws Exception
     */
    @Log("根据资源编号和类型查询")
    @RequestMapping("/getTravelFileBySourceIdAndType")
    @ResponseBody
    public Object getTravelFileBySourceIdAndType(TravelFile travelFile, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if (StringUtils.isNotBlank(travelFile.getSourceId()) && StringUtils.isNotBlank(travelFile.getStype())){
            result.setData(service.getTravelFileBySourceIdAndType(travelFile.getSourceId(), travelFile.getStype()));
        }else {
            result.setCode(-1);
            result.setInfo("单体编号或文件类型无效!");
        }
        return result;
    }

    /**
     * 获取分页数据
     * @param travelFile
     * @param request
     * @return
     * @throws Exception
     */
    @Log("分页查询旅游文件列表")
    @RequestMapping("/getTravelFilePage")
    @ResponseBody
    public Object getTravelFilePage(TravelFile travelFile, HttpServletRequest request) throws Exception{
        Page page = CommonUtil.getPageFromRequest(request);
        List<TravelFile> list = service.getTravelFilePage(travelFile, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询旅游文件")
    @RequestMapping("/getTravelFile")
    @ResponseBody
    public Object getTravelFile(String id, HttpServletRequest request) throws Exception{
        TravelFile travelFile = service.getTravelFile(id);
        return new Result(travelFile);
    }

    /**
     * 新增
     * @param travelFile
     * @param request
     * @return
     * @throws Exception
     */
    @Log("新增旅游文件")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(TravelFile travelFile, HttpServletRequest request) throws Exception{
        int num = service.insertTravelFile(travelFile);
        return new Result();
    }

    /**
     * 修改
     * @param travelFile
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改旅游文件")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(TravelFile travelFile, HttpServletRequest request) throws Exception{
        int num = service.updateTravelFile(travelFile);
        return new Result();
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("删除旅游文件")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            int num = service.deleteTravelFile(idArr);
        }else if(StringUtils.isNotBlank(id)){
            int num = service.deleteTravelFile(id);
        }else{
            result.setCode(-1);
            result.setInfo("删除失败:待删除编号无效!");
        }
        return result;
    }

    /**
     * 上传照片
     * @param file
     * @param sourceId
     * @param request
     * @return
     * @throws Exception
     */
    @Log("上传照片")
    @RequestMapping({"/uploadImg"})
    @ResponseBody
    public Object uploadImg(MultipartFile file, String sourceId, HttpServletRequest request) throws Exception {
        Result result = new Result();
        if (file != null && StringUtils.isNotBlank(sourceId)){
            FileInfo fileInfo = fileService.saveFile(file);//保存文件
            TravelFile travelFile = new TravelFile();
            travelFile.setSourceId(sourceId);//单体id
            travelFile.setFileId(fileInfo.getId());//文件id
            travelFile.setStype("1");//照片类型
            travelFile.setNum(System.currentTimeMillis());//时间戳为显示顺序
            service.insertTravelFile(travelFile);//插入记录
            travelFile.setFileInfo(fileInfo);
            List<TravelFile> list = new ArrayList<TravelFile>();
            list.add(travelFile);
            result.setData(list);
        }else{
            result.setCode(-1);
            result.setInfo("照片或单体编号无效!");
        }

        return result;
    }

    /**
     * 上传文件
     * @param file
     * @param sourceId
     * @param type 文件类型
     * @param request
     * @return
     * @throws Exception
     */
    @Log("上传文件")
    @RequestMapping({"/uploadFile"})
    @ResponseBody
    public Object uploadFile(MultipartFile file,String stype, String sourceId, HttpServletRequest request) throws Exception {
        Result result = new Result();
        if (file != null && StringUtils.isNotBlank(sourceId) && StringUtils.isNotBlank(stype)){
            FileInfo fileInfo = fileService.saveFile(file);//保存文件
            TravelFile travelFile = new TravelFile();
            travelFile.setSourceId(sourceId);//单体id
            travelFile.setFileId(fileInfo.getId());//文件id
            travelFile.setStype(stype);//文件类型,这里通过类型来区分多张表中的多个附件相关字段与文件表的关联
            travelFile.setNum(System.currentTimeMillis());//时间戳为显示顺序
            service.insertTravelFile(travelFile);//插入记录
            travelFile.setFileInfo(fileInfo);
            List<TravelFile> list = new ArrayList<TravelFile>();
            list.add(travelFile);
            result.setData(list);
        }else{
            result.setCode(-1);
            result.setInfo("文件/单体编号/文件类型无效!");
        }

        return result;
    }
}